import os
import sys
import transaction

from sqlalchemy import engine_from_config

from pyramid.paster import (
    get_appsettings,
    setup_logging,
)

from .models import DBSession, Student, Ftprecode, Base


def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri>\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)


def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    with transaction.manager:
        admin1 = Student()
        admin1.id = 1
        admin1.name = '张三'
        admin1.percent = 56

        admin2 = Student()
        admin2.id = 2
        admin2.name = '李四'
        admin2.percent = 66

        admin3 = Student()
        admin3.id = 3
        admin3.name = '王五'
        admin3.percent = 46

        DBSession.add_all([admin1, admin2, admin3])
